//
// Created by xili on 2024/8/8 19:03.
// Go big or go home.
//
#include <vector>
#include <unordered_map>

using namespace std;

class Solution {
public:
    int subarraysDivByK(vector<int> &nums, int k) {
        int ans = 0;
        int n = nums.size();
        int sum = 0;
        unordered_map<int, int> ump;
        //0这个前缀和在一个数都没有的时候就已经出现了
        ump[0] = 1;
        for (int i = 0; i < n; i++) {
            sum += nums[i];
            int m = (sum % k + k) % k;
            ans += ump[m];
            ump[m]++;
        }
        return ans;
    }
};